package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;
import com.mediway.hos.messgateway.model.entity.ServiceManage;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.validation.Valid;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.CodeAndMessageEnum;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.biz.entity.biz.SsInterSystems;
import work.mediway.mdm.biz.entity.biz.SsServiceLinkSystem;
import work.mediway.mdm.biz.service.biz.SsInterSystemsService;
import work.mediway.mdm.biz.vo.biz.ServiceManageVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2020/11/27
 */
@Api(tags = "对接系统模块")
@ApiSupport(order = 3)
@Validated
@RequestMapping("inter/sys")
@RestController
@RequiredArgsConstructor
public class InterSystemController {

    private final SsInterSystemsService interSystemsService;

    @OpsLog(value = "对接系统列表", type = OpsLogTypeEnum.SELECT)
    @PostMapping("list")
    @ApiOperation(value = "对接系统列表")
    @ApiOperationSupport(order = 1)
    public BaseResponse<Page<SsInterSystems>> listInterSystem(SsInterSystems interSystems, QueryRequest queryRequest) {
        Page<SsInterSystems> page = this.interSystemsService.listInterSystem(interSystems, queryRequest);
        return ObjectUtil.isNotEmpty(page) ? BaseResponse.success(page) : BaseResponse.error(String.valueOf(CodeAndMessageEnum.DB_RESOURCE_NULL.getCode()), CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
    }

    @OpsLog(value = "新增对接系统", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "新增对接系统", content = "新增对接系统")
    @PostMapping
    @ApiOperation(value = "新增对接系统")
    @ApiOperationSupport(order = 3)
    public BaseResponse<SsInterSystems> saveInterSystem(@Valid @RequestBody SsInterSystems interSystems) {
        this.interSystemsService.saveInterSystem(interSystems);
        return BaseResponse.success();
    }

    @OpsLog(value = "编辑对接系统", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "编辑对接系统", content = "编辑对接系统")
    @PostMapping("update")
    @ApiOperation(value = "编辑对接系统")
    @ApiOperationSupport(order = 5)
    public BaseResponse<SsInterSystems> updateInterSystem(@Valid @RequestBody SsInterSystems interSystems) {
        this.interSystemsService.updateInterSystem(interSystems);
        return BaseResponse.success();
    }

    @OpsLog(value = "删除对接系统", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "删除对接系统", content = "删除对接系统")
    @PostMapping("delete")
    @ApiOperation(value = "删除对接系统")
    @ApiOperationSupport(order = 7)
    public BaseResponse<SsInterSystems> deleteInterSystem(@RequestBody List<String> ids) {
        this.interSystemsService.deleteInterSystem(ids);
        return BaseResponse.success();
    }

    @OpsLog(value = "新增服务(重写)", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "新增服务(重写)", content = "新增服务(重写)")
    @PostMapping("service")
    @ApiOperation(value = "新增服务(重写)")
    @ApiOperationSupport(order = 9)
    public BaseResponse<String> saveInterService(@RequestBody SsServiceLinkSystem serviceLinkSystem) {
        this.interSystemsService.saveInterService(serviceLinkSystem);
        return BaseResponse.success();
    }

    @OpsLog(value = "编辑服务(重写)", type = OpsLogTypeEnum.UPDATE)
    @OperLog(title = "编辑服务(重写)", content = "编辑服务(重写)")
    @PostMapping("service/update")
    @ApiOperation(value = "编辑服务(重写)")
    @ApiOperationSupport(order = 11)
    public BaseResponse<String> updateInterService(@RequestBody SsServiceLinkSystem serviceLinkSystem) {
        this.interSystemsService.updateInterService(serviceLinkSystem);
        return BaseResponse.success();
    }

    @OpsLog(value = "查询服务(重写)", type = OpsLogTypeEnum.SELECT)
    @GetMapping("service")
    @ApiOperation(value = "查询服务(重写)")
    @ApiImplicitParam(value = "关联系统id", name = "ssslsSystemId", paramType = "query")
    @ApiOperationSupport(order = 13)
    public BaseResponse<Page<ServiceManageVO>> selectInterService(ServiceManage serviceManage, Long ssslsSystemId, QueryRequest queryRequest) {
        Page<ServiceManageVO> page = this.interSystemsService.selectInterService(serviceManage, ssslsSystemId, queryRequest);
        return BaseResponse.success(page);
    }

    @OpsLog(value = "删除服务(重写)", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "删除服务(重写)", content = "删除服务(重写)")
    @PostMapping("service/delete")
    @ApiOperation(value = "删除服务(重写)")
    @ApiImplicitParam(value = "关联系统id", name = "ssslsSystemId", paramType = "query")
    @ApiOperationSupport(order = 15)
    public BaseResponse<String> deleteInterService(@RequestBody List<String> ids) {
        this.interSystemsService.deleteInterService(ids);
        return BaseResponse.success();
    }

    @OpsLog(value = "根据服务id查询服务(重写)", type = OpsLogTypeEnum.SELECT)
    @GetMapping("service/{serviceId}")
    @ApiOperation(value = "根据服务id查询服务(重写)")
    @ApiOperationSupport(order = 13)
    public BaseResponse<ServiceManageVO> selectInterServiceByServiceId(@PathVariable("serviceId") Long serviceId) {
        ServiceManageVO serviceManageVO = this.interSystemsService.selectInterServiceByServiceId(serviceId);
        return BaseResponse.success(serviceManageVO);
    }

}
